﻿using Novacode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetFunctionDemo.CommonLib
{
    public class DocxHelper
    {
        /// <summary>
        /// 替换word内容 把word可以当作模版
        /// </summary>
        /// <param name="file"></param>
        public static void OpenWord(string file)
        {
            DocX word = DocX.Load(file);
            //获取申请信息
            string[] keys = new string[] { "CompName", "TheApplicant", "CostCenter", "SealNames" };
            foreach (var item in keys)
            {
                var patter = "{{" + item + "}}";
                var par = word.Paragraphs.FirstOrDefault(m => m.Text.Contains(patter));
                par.ReplaceText(patter, "");
            }
            word.Save();
        }

        public static void NewWord(string fileName)
        {
           
            DocX doc = DocX.Create(fileName, DocumentTypes.Document);
            //插入一个段落
            Paragraph par = doc.InsertParagraph();
            par.Color(System.Drawing.Color.Red);
            par.FontSize(16);
            par.Bold();
            par.InsertText("段落练习！");
            doc.Save();
        }

    }
}
